﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using AKJ.Sharepoint.Branding.Config;
using AKJ.Sharepoint.Branding.Code;
using System.Web.UI;

namespace AKJ.Sharepoint.Branding.BrandingInterceptor
{
    public class QuerystringInterceptor : IBrandingInterceptor
    {
        public void InterceptRequest(Page page, HttpRequest request)
        {
            // If we page did't have a master then abort
            if (page == null)
                return;
                        
            // Open the custom Visual Branding Section 
            VisualBrandingSection visualBrandingSection = VisualBrandingSection.Instance();

            foreach (QuerystringElement queryElement in visualBrandingSection.QuerystringBrandings)
            {
                string value = request.QueryString[queryElement.Parameter];

                if (string.IsNullOrEmpty(value) == false)
                {
                    if (value.ToLower() == queryElement.Value.ToLower())
                    {
                        if (queryElement.ApplyCss.File != "")
                            BrandingManager.InsertBrandingElement(BrandingElementType.AKJ_Sharepoint_Css, int.MaxValue, queryElement.ApplyCss.File);

                        if (queryElement.ApplyMaster.File != "")
                            BrandingManager.InsertBrandingElement(BrandingElementType.AKJ_Sharepoint_Master, int.MaxValue, queryElement.ApplyMaster.File);                    
                    }
                }
            }
        }        
    }
}
